package com.pentajock.docdb.dialogs;

/*
 * Liscense: 	GPL http://www.gnu.org/copyleft/gpl.html
 * By: 		A. Tres Finocchiaro
 * Date:	2007
 * Java:	6.0SE
 */

import com.pentajock.docdb.Main;
import com.pentajock.docdb.lang.Lang;
import javax.swing.*;
import java.util.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Cursor;
import java.util.concurrent.atomic.*;

public class ProgressDialog {
	/*
	 * GUI
	 */
	 
	private JTextArea textArea;
	private JScrollPane textScrollPane;
	private JDialog thisDialog;
	private JOptionPane thisPane;
	private JProgressBar progressBar;
	
	private final String COLON = Lang.MISC.getText("CHOICE_DELIMITER", ":");
	private final String DOC_PROC = Lang.STATUS_BAR.getText("DOCUMENTS_PROCESSED", "documents processed");
	private final String DOC_FOUND = Lang.STATUS_BAR.getText("DOCUMENTS_FOUND", "documents found");
	private final String DONE_COUNT = Lang.STATUS_BAR.getText("DONE_COUNTING", "(Done Counting)");
	private final String PLEASE_WAIT = "\n\n\n" + Lang.STATUS_BAR.getText("ITERATING_RESULTS", "Done counting. " 
		+ "Iterating.  May take 20 minutes per 1000 docs") + "\n\n";
	
	/*
	 * ETC
	 */
	 
	private AtomicInteger atomicCounter;
	private AtomicReference<String> atomicString;
	
	public ProgressDialog(Component parent) {
		initializeObjects(parent);
		createAndShowGUI();
	}
	
	private void initializeObjects(Component parent) {
		atomicCounter = new AtomicInteger(0);
		atomicString = new AtomicReference<String>("");
		
		progressBar = new JProgressBar();
		progressBar.setIndeterminate(true);
		progressBar.setStringPainted(true);
		progressBar.setString(Lang.DIALOG.getText("DOCUMENT_COUNTING", "Counting..."));
		
		textArea = new JTextArea(Lang.DIALOG.getText("DOCUMENT_COUNTING", "Counting supported Documents, please wait..."));
		textScrollPane = new JScrollPane(textArea);
		
		Object[] dialogObjects = new Object[]{progressBar, textScrollPane};
		thisPane = new JOptionPane(dialogObjects, JOptionPane.WARNING_MESSAGE, JOptionPane.CANCEL_OPTION);
		thisDialog = thisPane.createDialog(parent, Lang.MISC.getText("PROGRESS", "Progress"));
	}
	
	private void createAndShowGUI() {
		thisDialog.setModal(false);
		thisDialog.setResizable(true);
		thisDialog.setPreferredSize(new Dimension(600, 300));
		//thisDialog.pack();
		setVisible(true);
	}
	
	/*
	 * Sets visibility on an thread-safe event
	 */
	public void setVisible(final boolean visible) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (visible)
					thisDialog.pack();
				thisDialog.setVisible(visible);
			}
		});
		setWaitCursor(true);
	}
	
	/*
	 * Sets the cursor on an thread-safe event
	 */
	public void setWaitCursor(final boolean waitCursor) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {		
				if (waitCursor)
					Main.setCursor(thisDialog, Cursor.WAIT_CURSOR);
				else
					Main.setCursor(thisDialog, Cursor.DEFAULT_CURSOR);
			}
		});		
	}
	
	/*
	 * Sets counter, then updates dialog on a thread-safe event
	 */
	public void setCounter(int counter) {
		atomicCounter.set(counter);
		updateDialog();
	}
	
	/*
	 * Increments counter, then updates dialog on thread-safe event
	 */
	public int incrementCounter() {
		int temp = atomicCounter.incrementAndGet();
		updateDialog();
		return temp;
	}
	
	/*
	 * Decrements counter, then updates dialog on thread-safe event
	 */
	public int decrementCounter() {
		int temp = atomicCounter.decrementAndGet();
		updateDialog();
		return temp;
	}
	
	/*
	 * Resets counter, then updates dialog on thread-safe event
	 */
	public void resetCounter() {
		atomicCounter.set(0);
		updateDialog();
	}
	
	/*
	 * Sets the message, then updates dialog on a thread-safe event
	 */
	public void addMessage(String message) {
		atomicString.set(message);
		updateDialog();
	}
	
	/*
	 * Thread-save dialog updater
	 */
	private void updateDialog() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				int counter = atomicCounter.get();
				
				textArea.setText(textArea.getText() +(String)atomicString.get());
				progressBar.setValue(counter);
				if (counter < 0)
					progressBar.setString(-counter + " " + DOC_FOUND);
				else if (counter > 0)
					progressBar.setString(counter + " " + DOC_PROC);
				else
					textArea.setText(textArea.getText() + PLEASE_WAIT);
			}
		});
	}
	
	/*
	 * Returns JOptionPane.OK_OPTION or CANCEL_OPTION
	 */
	public int getValue() {
		try {
			return ((Integer)thisPane.getValue()).intValue();
		}
		catch (Exception e) {
			return -1;
		}
	}
	
	public void setMaxValue(final int maxValue) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				progressBar.setMaximum(maxValue);
				progressBar.setValue(atomicCounter.get());
				progressBar.setIndeterminate(false);
			}
		});
	}
}
